/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChatSystem;

import ChateoController.*;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
import ChateoController.ChatViewController;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * This class is the main class from which will be launched our ChatSystem
 *
 * @author adrian
 * @author olga
 */
public class ChatSystem extends Application {

    private static ChatViewController cvcontroller;
    private static Controller controller;

    public static void main(String[] args) throws Exception {
        boolean GUI = false;
        //With GUI
        if (GUI) {
            controller = new Controller();
            launch(args);
        } //Without GUI
        else {
            CLI();
        }
    }

    /**
     * This method is aimed in launching the program from the non graphical
     * point of view
     */
    public static void CLI() {
        /**
         *
         */

        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("Entrez votre login");
            String str = sc.nextLine();
            System.out.println("Bienvenue " + str);
            System.out.println("Veuillez maintenant entrer le port du serveur");
            int port = Integer.parseInt(sc.nextLine());
            Controller controller1 = new Controller(str, port);
            controller1.connect();
            while (true) {
                System.out.println("Enter 1 to send a message, enter something else to disconnect");
                String choice = sc.nextLine();
                if (choice.equals("1")) {
                    System.out.println("This is the list of connected users " + controller1.getUserList());
                    System.out.println("Enter the login of your friend");
                    String Dest = sc.nextLine();
                    System.out.println("Enter your message");
                    String Mensaje = sc.nextLine();
                    System.out.println("Enter 1 to send a private message, enter something else to send a broadcast message");
                    String publico = sc.nextLine();
                    if (publico.equals("1")) {
                        controller1.mensajesBasicos(Mensaje, Dest, true);
                    } else {
                        controller1.mensajesBasicos(Mensaje, Dest, false);
                    }

                } else {
                    controller1.disconnect();
                }
            }

        } catch (NumberFormatException nfe) {
            System.out.println("NFE");

        } catch (SocketException se) {
            System.out.println("SE");
        } catch (UnknownHostException uhe) {
            System.out.println("UHE");
        }

    }
    
    /**
     * This method is aimed in closing the view window after 
     * clicking on the exit icon
     * @throws Exception 
     */
    @Override
    public void stop() throws Exception {
        cvcontroller.onExit();
        super.stop();
    }
    /**
     * This method is aimed in launching the GUI part
     * @param stage 
     */

    @Override
    public void start(Stage stage) {

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/ChateoGUI/ChatRoom.fxml"));
            Parent root = loader.load();
            cvcontroller = loader.getController();
            Controller.setcvController(cvcontroller);
            Scene scene = new Scene(root);

            stage.setScene(scene);
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(ChatSystem.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
